% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Replication "Deconstructing the Yield Curve"
% Crump and Gospodinov (2024)
% Date: 26-JUL-2024
% Code to make csv files for all simulation tables
%   without external predictors
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
clc;
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SETTINGS
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alph = 0.10;
dispPref = '%3.3f';
%
fileTag1 = 'VAR1';
%fileTag1 = 'VAR2';
%fileTag2 = '5Fac_S5000_T600';
fileTag2 = '5Fac_S5000_T2000';
%fileTag2 = '3Fac_S5000_T600';
%
bc = load(['../output/sims', fileTag1, 'BC_', fileTag2]);
nobc = load(['../output/sims', fileTag1, 'noBC_', fileTag2]);
%
mats = {'2y', '3y', '4y', '5y', '6y', '7y', '8y', '9y', '10y'};
%
size_bc_CG = squeeze(mean(bc.pval_Size_CG < alph));
size_nobc_CG = squeeze(mean(nobc.pval_Size_CG < alph));
size_bc_BH = squeeze(mean(bc.pval_Size_BH < alph));
size_nobc_BH = squeeze(mean(nobc.pval_Size_BH < alph));
size_bc_FullOracle = squeeze(mean(bc.pval_Size_FullOracle < alph));
%
power_bc_CG = squeeze(mean(bc.pval_Power_CG < alph));
power_nobc_CG = squeeze(mean(nobc.pval_Power_CG < alph));
power_bc_BH = squeeze(mean(bc.pval_Power_BH < alph));
power_nobc_BH = squeeze(mean(nobc.pval_Power_BH < alph));
power_bc_FullOracle = squeeze(mean(bc.pval_Power_FullOracle < alph));
%
[m,k] = size(size_bc_CG(2:end,2:end));
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BOOTSTRAP METHODS
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tab_size_bc = strings(m,3*k+1);
tab_power_bc = strings(m,3*k+1);
tab_size_nobc = strings(m,2*k+1);
tab_power_nobc = strings(m,2*k+1);
%
tab_size_bc(:,1) = mats;
tab_power_bc(:,1) = mats;
tab_size_nobc(:,1) = mats;
tab_power_nobc(:,1) = mats;
%
tmp_size_bc = [size_bc_CG(2:end,2:end) size_bc_BH(2:end,2:end) size_bc_FullOracle(2:end,2:end)];
tmp_power_bc = [power_bc_CG(2:end,2:end) power_bc_BH(2:end,2:end) power_bc_FullOracle(2:end,2:end)];

tmp_size_nobc = [size_nobc_CG(2:end,2:end) size_nobc_BH(2:end,2:end)];
tmp_power_nobc = [power_nobc_CG(2:end,2:end) power_nobc_BH(2:end,2:end)];
%
for i = 1:width(tmp_size_bc), tab_size_bc(:,i+1) = num2str(round(tmp_size_bc(:,i),3),dispPref); end
for i = 1:width(tmp_power_bc), tab_power_bc(:,i+1) = num2str(round(tmp_power_bc(:,i),3),dispPref); end    
for i = 1:width(tmp_size_nobc), tab_size_nobc(:,i+1) = num2str(round(tmp_size_nobc(:,i),3),dispPref); end    
for i = 1:width(tmp_power_nobc), tab_power_nobc(:,i+1) = num2str(round(tmp_power_nobc(:,i),3),dispPref); end    
    
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ASYMPTOTIC STANDARD ERRORS
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
size_NW = squeeze(mean(abs(bc.tstat_Size_NW) > norminv(1-alph/2),1));
power_NW = squeeze(mean(abs(bc.tstat_Size_NW) > norminv(1-alph/2),1));

size_EWC = squeeze(mean(abs(bc.tstat_Size_LLSW_EWC) > tinv(1-alph/2,bc.sp.nu_LLSW_EWC),1));
power_EWC = squeeze(mean(abs(bc.tstat_Power_LLSW_EWC) > tinv(1-alph/2,bc.sp.nu_LLSW_EWC),1));

if alph == 0.1
    cv_LLSW_NW_10 = load('../input/cv_nw_10.mat');
    [~, idx] = min(abs(bc.sp.S_LLSW_NW/bc.sp.T-cv_LLSW_NW_10.cv_nw_10(:,1,1)));
    size_NW_LLSW = squeeze(mean(abs(bc.tstat_Size_LLSW_NW) > sqrt(cv_LLSW_NW_10.cv_nw_10(idx,2,1)),1));
    power_NW_LLSW = squeeze(mean(abs(bc.tstat_Power_LLSW_NW) > sqrt(cv_LLSW_NW_10.cv_nw_10(idx,2,1)),1));
elseif alph == 0.05
    cv_LLSW_NW_05 = load('../input/cv_nw_05.mat');
    [~, idx] = min(abs(bc.sp.S_LLSW_NW/bc.sp.T-cv_LLSW_NW_05.cv_nw_05(:,1,1)));
    size_NW_LLSW = squeeze(mean(abs(bc.tstat_Size_LLSW_NW) > sqrt(mean(cv_LLSW_NW_05.cv_nw_05(idx,2,1))),1));
    power_NW_LLSW = squeeze(mean(abs(bc.tstat_Power_LLSW_NW) > sqrt(mean(cv_LLSW_NW_05.cv_nw_05(idx,2,1))),1));
end

tab_size_LLSW = strings(m,3*k+1);
tab_power_LLSW = strings(m,3*k+1);

tmp_size_LLSW = [size_NW(2:end,2:end) size_EWC(2:end,2:end) size_NW_LLSW(2:end,2:end)];
tmp_power_LLSW = [power_NW(2:end,2:end) power_EWC(2:end,2:end) power_NW_LLSW(2:end,2:end)];

tab_size_LLSW(:,1) = mats;
for i = 1:width(tmp_size_LLSW), tab_size_LLSW(:,i+1) = num2str(round(tmp_size_LLSW(:,i),3),dispPref); end
tab_power_LLSW(:,1) = mats;
for i = 1:width(tmp_power_LLSW), tab_power_LLSW(:,i+1) = num2str(round(tmp_power_LLSW(:,i),3),dispPref); end

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SAVE CSV FILES
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alphTag = num2str(100*alph);
%
writematrix(tab_size_bc, ['../output/TableSimsSize_', fileTag1, '_BC_', fileTag2, '_alpha', alphTag, '.csv']);
writematrix(tab_size_nobc, ['../output/TableSimsSize_', fileTag1, '_noBC_', fileTag2, '_alpha', alphTag, '.csv']);
writematrix(tab_power_bc, ['../output/TableSimsPower_', fileTag1, '_BC_', fileTag2, '_alpha', alphTag, '.csv']);
writematrix(tab_power_nobc, ['../output/TableSimsPower_', fileTag1, '_noBC_', fileTag2, '_alpha', alphTag, '.csv']);
writematrix(tab_size_LLSW, ['../output/TableSimsSize_', fileTag1, '_LLSW_', fileTag2, '_alpha', alphTag, '.csv']);
writematrix(tab_power_LLSW, ['../output/TableSimsPower_', fileTag1, '_LLSW_', fileTag2, '_alpha', alphTag, '.csv']);



